package controllers;

import com.google.gson.JsonObject;
import constants.TplPath;
import constants.system.MemberShip;
import constants.ws.PageBean;
import duppentity.dto.bu.BuDtoUtil;
import duppentity.dto.bu.CouponUserDto;
import duppentity.dto.merchant.BuMerchantCouponDto;
import duppentity.dto.merchant.BuMerchantCouponDtoUtil;
import duppentity.dto.merchant.SysAdminDto;
import duppentity.dto.merchant.SysAdminDtoUtil;

import utils.RequestHelper;
import utils.Utils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Description:
 * Author: pengpeng@djcars.cn
 * Date: 2019/12/10 9:58
 */

public class MerchantController extends ApiController{

    /**
     *  商家优惠券
     */
    public static void merchantList() {
        String page = TplPath.merchantList;
        if (Utils.exitFile(page)) {
            renderTemplate(page);
        }
    }

    public static void getMerchantList() {
        PageBean pageBean = PageBean.get();

        JsonObject data = merchantDupp.getMerchantList(1, pageBean.getOffset(), pageBean.pageCount());
        if (!checkResult(data)) {
            apiError(data);
        }
        List<SysAdminDto> sysAdminList = SysAdminDtoUtil.getSysAdminDtoList(data, false);
        apiList(data, sysAdminList);
    }

    public static void addMerchant() {
        String page = TplPath.addMerchant;
        if (Utils.exitFile(page)) {
            renderTemplate(page);
        }
    }

    public static void saveMerchant() {
        String mobile = params.get("mobile", String.class);
        String name = params.get("name", String.class);

        JsonObject data = merchantDupp.saveMerchant(1, name, mobile, 0L, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("id").getAsInt() > 0) {
                apiOk("新增商家账号成功");
            } else {
                apiError("新增商家账号失败");
            }
        } else {
            apiError(data);
        }
    }

    /**
     * 商家的子账号列表
     */
    public static void subAccountList() {
        Long parentId = params.get("parentId", Long.class);
        String page = TplPath.subAccountList;
        if (Utils.exitFile(page)) {
            renderTemplate(page, parentId);
        }
    }

    public static void getListByParentId() {
        Long parentId = params.get("parentId", Long.class);
        PageBean pageBean = PageBean.get();

        JsonObject data = merchantDupp.getListByParentId(parentId, 1, pageBean.getOffset(), pageBean.pageCount());

        if (checkResult(data)) {
            apiOk(data);
        } else {
            apiError(data);
        }
    }

    public static void updateInvalid() {
        Long id = params.get("id", Long.class);
        Integer invalid = params.get("invalid", Integer.class);

        JsonObject data = merchantDupp.updateInvalid(id, invalid, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("result").getAsBoolean()) {
                apiOk("修改账号状态成功");
            } else {
                apiError("修改账号状态失败");
            }
        } else {
            apiError(data);
        }
    }

    /**
     * 商家的优惠券配置
     */
    public static void merchantCouponList() {
        Long merchantId = params.get("merchantId", Long.class);
        String merchantName = params.get("merchantName", String.class);
        String page = TplPath.merchantCouponList;
        if (Utils.exitFile(page)) {
            renderTemplate(page, merchantId, merchantName);
        }
    }

    public static void getMerchantCouponList() {
        PageBean pageBean = PageBean.get();
        Long merchantId = params.get("merchantId", Long.class);

        JsonObject data = merchantDupp.getMerchantCouponList(merchantId, pageBean.getOffset(), pageBean.pageCount());
        if (!checkResult(data)) {
            apiError(data);
        }
        List<BuMerchantCouponDto> buMerchantCouponList = BuMerchantCouponDtoUtil.getBuMerchantCouponDtoList(data);
        apiList(data, buMerchantCouponList);
    }

    public static void addMerchantCoupon() {
        Long merchantId = params.get("merchantId", Long.class);
        String merchantName = params.get("merchantName", String.class);
        JsonObject couponList = couponDupp.getCouponList(null, null, null, 1, 0, Integer.MAX_VALUE);

        String page = TplPath.addMerchantCoupon;
        if (Utils.exitFile(page)) {
            renderTemplate(page, couponList, merchantId, merchantName);
        }
    }

    public static void saveMerchantCoupon() {
        Long merchantId = params.get("merchantId", Long.class);
        Long couponId = params.get("couponId", Long.class);
        Integer invalid = params.get("invalid", Integer.class);

        JsonObject data = merchantDupp.saveMerchantCoupon(merchantId, couponId, invalid, 0, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("id").getAsInt() > 0) {
                apiOk("新增商家的优惠配置成功");
            } else {
                apiError("新增商家的优惠配置失败");
            }
        } else {
            apiError(data);
        }
    }

    public static void updateCouponInValid() {
        Long id = params.get("id", Long.class);
        Integer invalid = params.get("invalid", Integer.class);

        JsonObject data = merchantDupp.updateCouponInValid(id, invalid, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("result").getAsBoolean()) {
                apiOk("修改展示状态成功");
            } else {
                apiError("修改展示状态失败");
            }
        } else {
            apiError(data);
        }
    }

    /**
     * 商家优惠券列表(派发概览)
     */
    public static void allMerchantCouponList() {
        String page = TplPath.allMerchantCouponList;
        if (Utils.exitFile(page)) {
            renderTemplate(page);
        }
    }

    public static void getAllMerchantCouponList() {
        PageBean pageBean = PageBean.get();

        JsonObject data = merchantDupp.getAllMerchantCouponList(MemberShip.current().getId(), null, pageBean.getOffset(), pageBean.pageCount());
        if (!checkResult(data)) {
            apiError(data);
        }
        List<BuMerchantCouponDto> buMerchantCouponList = BuMerchantCouponDtoUtil.getBuMerchantCouponDtoList(data);
        apiList(data, buMerchantCouponList);
    }

    /**
     *  商家优惠券明细
     */
    public static void merchantCouponDetails() {
        Long couponId = params.get("couponId",Long.class);
        String merchantName = params.get("merchantName",String.class);
        String couponName = params.get("couponName",String.class);
        String page = TplPath.merchantCouponDetails;
        if (Utils.exitFile(page)) {
            renderTemplate(page, couponId, merchantName, couponName);
        }
    }

    public static void getMerchantCouponDetails() {
        PageBean pageBean = PageBean.get();
        Long couponId = params.get("couponId",Long.class);
        String mobile = params.get("mobile", String.class);
        String dispatchUserName = params.get("dispatchUserName", String.class);
        String startCreateTime = params.get("startCreateTime", String.class);
        String endCreateTime = params.get("endCreateTime", String.class);
        int type = RequestHelper.getIntParam("type");
        if(Utils.isNullOrEmpty(startCreateTime) || Utils.isNullOrEmpty(endCreateTime)){
            if(type != 0){
                apiError("筛选时间时类型和日期必须都选择");
            }
        }
        if(type == 0){
            if (!Utils.isNullOrEmpty(startCreateTime) || !Utils.isNullOrEmpty(endCreateTime)) {
                apiError("筛选时间时类型和日期必须都选择");
            }
        }
        JsonObject data = null;
        if (type == 1) {
            data = merchantDupp.getCouponUserList(dispatchUserName, mobile, couponId, startCreateTime, endCreateTime, null, null, null, null, pageBean.getOffset(), pageBean.pageCount());
        } else if (type == 2) {
            data = merchantDupp.getCouponUserList(dispatchUserName, mobile, couponId, null, null, null, null, startCreateTime, endCreateTime, pageBean.getOffset(), pageBean.pageCount());
        } else if (type == 3) {
            data = merchantDupp.getCouponUserList(dispatchUserName, mobile, couponId, null, null, startCreateTime, endCreateTime, null, null, pageBean.getOffset(), pageBean.pageCount());
        } else {
            data = merchantDupp.getCouponUserList(dispatchUserName, mobile, couponId, null, null, null, null, null, null, pageBean.getOffset(), pageBean.pageCount());
        }

        if (!checkResult(data)) {
            apiError(data);
        }
        List<CouponUserDto> couponUserDtoList = BuDtoUtil.getCouponUserDtoList(data);
        apiList(data, couponUserDtoList);
    }

    /**
     *  商家优惠券数据报表
     */
    public static void merchantCouponStat() {
        String merchantName = params.get("merchantName",String.class);
        String couponName = params.get("couponName",String.class);
        Long couponId = params.get("couponId",Long.class);
        String page = TplPath.merchantCouponStat;
        if (Utils.exitFile(page)) {
            renderTemplate(page, couponId, merchantName, couponName);
        }
    }

    public static void getListByYear() {
        PageBean pageBean = PageBean.get();
        Long couponId = params.get("couponId", Long.class);
        Integer year = params.get("year", Integer.class);
        if (year == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            year = calendar.get(Calendar.YEAR);
        }

        JsonObject data = merchantDupp.getListByYear(couponId, year, pageBean.getOffset(), pageBean.pageCount());

        if (checkResult(data)) {
            apiOk(data);
        } else {
            apiError(data);
        }
    }

    public static void getListByMonth() {
        PageBean pageBean = PageBean.get();
        Long couponId = params.get("couponId", Long.class);
        Integer year = params.get("year", Integer.class);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        Integer month = params.get("month", Integer.class);
        if (month == null) {
            month = calendar.get(Calendar.MONTH) + 1;
        }

        JsonObject data = merchantDupp.getListByMonth(couponId, year, month, pageBean.getOffset(), pageBean.pageCount());

        if (checkResult(data)) {
            apiOk(data);
        } else {
            apiError(data);
        }
    }

}

